// pages/marketing/roleSet.js
const app = getApp();
var topOpacity = 0;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    // 组件参数设置，传递到组件
    defaultData: {
      title: "身份设置", // 导航栏标题
      showHome: false, //是否显示回到首页按钮
      showBack: true, //是否显示返回按钮
      showLogo: false, //是否显示logo
      bgColor: 'transparent'
    },
    baseUrl: app.baseUrl,
    version: app.globalData.version,
    navBarHeight: app.globalData.navBarHeight,
     // 姓名
     name: '',
     // 电话
     phone: '',
    role: '身份类型',
    roleList: [{
      id: 0,
      name: '麓客'
    }, {
      id: 1,
      name: '内部员工'
    }],

    qd: '您从哪获知此小程序',
    qdList: [{
      id: 0,
      name: '无'
    }, {
      id: 1,
      name: '陈俊男'
    }, {
      id: 2,
      name: '淦婷'
    }, {
      id: 3,
      name: '蒋玉莹'
    }, {
      id: 4,
      name: '李思维'
    }, {
      id: 5,
      name: '田欣'
    }, {
      id: 6,
      name: '杨顺'
    }, {
      id: 6,
      name: '袁宝'
    }, {
      id: 7,
      name: '杨佩璇'
    }, {
      id: 8,
      name: '郑闯'
    }],

    sex:'性别',
    sexList:[
      {id: 0,name:'男'},
      {id: 1,name:'女'},
    ],
    cardId:'',//身份证号
    accountBank:'',//开户行名称
    bankCard:'',//银行卡号
    openid: '',
    shareTitle: '',
    shareImg: '',
    disable:false
  },
  onPageScroll(t) {
    console.log(t)
    if (t.scrollTop < 40) {
      topOpacity = 0
      if (this.data.topOpacity != 0) {
        this.setData({
          topOpacity: 0
        })
      }
    } else if (t.scrollTop >= 40) {
      if (topOpacity <= 1) {
        topOpacity = t.scrollTop / 100 - .4
      } else {
        topOpacity = 1
      }
      if (topOpacity <= 1) {
        this.setData({
          topOpacity: topOpacity
        })
      }
    }
  },
  sexChange(e){
    this.setData({
      sex: this.data.sexList[e.detail.value].name,
    })
  },
    qdChange(e){
    this.setData({
      qd: this.data.qdList[e.detail.value].name,
    })
  },
    // 姓名
    nameIpt(e) {
      this.setData({
        name: e.detail.value
      })
    },
    // 电话
    phoneIpt(e) {
      this.setData({
        phone: e.detail.value
      })
    },
  //选择参加人数
  roleChange(e) {
    let that = this;
    that.setData({
      role: that.data.roleList[e.detail.value].name,
    })
  },

  //提交身份信息
  tj(){
    let that = this;
    if (that.data.name.trim() === '') {
      app.showTip('请输入姓名');
    } else if (that.data.phone.trim() === '') {
      app.showTip('请输入电话');
    } else if (isNaN(that.data.phone) || that.data.phone.length !== 11) {
      app.showTip('请输入正确电话');
    } else if (that.data.sex === '性别') {
      app.showTip('请选择性别');
    } else if (that.data.role === '身份类型') {
      app.showTip('请选择身份类型');
    } else if (that.data.cardId.trim() != ''&&that.data.cardId.trim().length != 18) {
      app.showTip('请输入18位身份证号码');
    }  else if (that.data.qd === '您从哪获知此小程序') {
      app.showTip('请选择获取渠道');
    } else {
      wx.showLoading({
        title: '数据提交中...'
      });
      
      wx.request({
        url: that.data.baseUrl + 'addMarketingSf.php',
        method: 'POST',
        data: {
          'openid': that.data.openid,
          'name':that.data.name.trim(),
          'tel':that.data.phone.trim(),
          'sf': that.data.role,
          'sex': that.data.sex,
          'idcard': that.data.cardId.trim(),
          'bank_name': that.data.accountBank.trim(),
          'bank_card':that.data.bankCard.trim(),
          'get_qd':that.data.qd
        },
        header: {
          'content-type': 'application/x-www-form-urlencoded'
        },
        success: function(res) {
          wx.hideLoading();
          if (res.data.status == 1) {
            //后台请求成功 跳转至首页
            app.showTip(res.data.msg);
            setTimeout(function(){
              wx.redirectTo({
                url: '/pages/marketing/index'
              });
            },1300)
        } else {
          app.showTip(res.data.msg);
        }
      },
      fail: function() {
        wx.hideLoading();
        app.showTip('抱歉,服务器繁忙');
      }
    });
    }
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    var that=this;

    if(options.look==2){
      that.setData({
        ['defaultData.title']: '身份详情'
      });
    }
   
    var openid = wx.getStorageSync('ly24_openid');
    if (openid == '') {
      //缓存中没有openid
      //获取所需页面信息
      wx.login({
        success: function(res) {
          if (res.code) {
            //发起网络请求
            wx.request({
              url: that.data.baseUrl + 'getOpenid.php',
              method: 'POST',
              data: {
                'code': res.code
              },
              header: {
                'content-type': 'application/x-www-form-urlencoded'
              },
              success: function(res) {
                //缓存微信openid
                wx.setStorageSync('ly24_openid', res.data.openid);
                that.setData({
                  openid: res.data.openid
                });

                //获取本页数据
                if(options.look==2){
                  //查看身份详情
                  that.getMarketingSfDetailPageInfo(res.data.openid);
                }else{
                  that.getMarketingRoleSetPageInfo(res.data.openid);
                }
              },
              fail: function(res) {}
            });
          }
        }
      });
    } else {
      that.setData({
        openid: openid
      });

      //获取本页数据
      if(options.look==2){
        //查看身份详情
        that.getMarketingSfDetailPageInfo(openid);
      }else{
        that.getMarketingRoleSetPageInfo(openid);
      }
    }

    //调用分享
    that.getShareInfo();
  },

  //获取本页数据
  getMarketingRoleSetPageInfo(openid){
    var that=this;
    wx.showLoading({
      title: '数据加载中...'
    })
    wx.request({
      url: that.data.baseUrl + 'getMarketingRoleSetPageInfo.php',
      method: 'POST',
      data: {
        'openid': openid
      },
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      success: function(res) {
        wx.hideLoading();
        if (res.data.status == 1) {
          //后台请求成功
        } else {
          app.showTip(res.data.msg);
          setTimeout(function() {
            wx.navigateBack({
              delta: 1
            });
          }, 2000);
        }
      },
      fail: function() {
        wx.hideLoading();
        app.showTip('抱歉,服务器繁忙');
        setTimeout(function() {
          wx.navigateBack({
            delta: 1
          });
        }, 2000);
      }
    });
  },

  //获取身份信息
  getMarketingSfDetailPageInfo(openid){
    var that=this;
    wx.showLoading({
      title: '数据加载中...'
    })
    wx.request({
      url: that.data.baseUrl + 'getMarketingSfDetailPageInfo.php',
      method: 'POST',
      data: {
        'openid': openid
      },
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      success: function(res) {
        wx.hideLoading();
        if (res.data.status == 1) {
          //后台请求成功
          var sfInfo = res.data.sfInfo;
          that.setData({
            disable:true,
            sex:sfInfo['sex'],//性别
            cardId:sfInfo['idcard'],//身份证号
            accountBank:sfInfo['bank_name'],//开户行名称
            bankCard:sfInfo['bank_card'],//银行卡号
            role: sfInfo['sf'],// 身份
            name: sfInfo['name'],// 姓名
            phone: sfInfo['tel'],// 电话
            qd:sfInfo['get_qd']//获取渠道
          });
        } else {
          app.showTip(res.data.msg);
          setTimeout(function() {
            wx.navigateBack({
              delta: 1
            });
          }, 2000);
        }
      },
      fail: function() {
        wx.hideLoading();
        app.showTip('抱歉,服务器繁忙');
        setTimeout(function() {
          wx.navigateBack({
            delta: 1
          });
        }, 2000);
      }
    });
  },

      //获取微信分享信息
  getShareInfo() {
    var that = this;
    wx.request({
      url: that.data.baseUrl + 'getShareInfo.php',
      method: 'POST',
      data: {

      },
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      success: function(res) {
        that.setData({
          shareTitle: res.data.shareTitle,
          shareImg: res.data.shareImg
        });
      },
      fail: function(res) {}
    });
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {
    var that = this;
    return {
      title: that.data.shareTitle,
      path: '/pages/index/index',
      imageUrl: that.data.shareImg,
      success: function(res) {

      },
      fail: function(res) {

      }
    }
  }
})